#pragma once 

#include <iostream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "log.hpp"
#include "protocol.hpp"

#define NUM 1024

class calclient
{
public:
    calclient(const std::string &serverip, const uint16_t &serverport)
        :_sock(-1), _serverip(serverip), _serverport(serverport)
    {}
    void initclient()
    {
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            exit(2);
        }
        // 2. tcp的客户端要不要bind？要的！ 要不要显示的bind？不要！这里尤其是client port要让OS自定随机指定！
        // 3. 要不要listen？不要！
        // 4. 要不要accept? 不要！
        // 5. 要什么呢？？要发起链接！
    }
    void start()
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(_serverport);
        server.sin_addr.s_addr = inet_addr(_serverip.c_str());
        //至此创建客户端的sock通信结构体信息创建完毕
        //开始进行链接，告诉服务器，我是客户端，并且告诉它我的信息
        if(!connect(_sock, (struct sockaddr*)&server, sizeof(server)))
        {
            std::cout << "socket connect error!" << std::endl;
        }
        else
        {
            //已经创建好了链接，那么就要开始进行输入任务，让服务器来执行了
            std::string line;
            std::string inbuffer;
            while(true)
            {
                std::cout << "mycal>>>";
                std::getline(std::cin, line);//1+1
                Request req = ParseLine(line);//"1+1"   //对输入的数据进行封装处理
                std::string content;
                req.serialize(&content);
                std::string send_string = enLength(content);
                send(_sock, send_string.c_str(), send_string.size(), 0);//BUG
                //至此，客服端发送的数据已经完成

                //接下来就是接收服务端回响的数据
                std::string package, text;
                if (!recvPackage(_sock, inbuffer, &package));
                    continue;
                if (!deLength(package, &text))
                    continue;
                Response resp;
                resp.deserialize(text);
                std::cout << "exitcode: " << resp.exitcode << endl;
                std::cout << "result: " << resp.result << endl;
            }
        }
    }
    Request ParseLine(const std::string &line)
    {
        //简易版本的状态机
        //0:操作符之前，1:碰到了操作符 2:操作符之后
        int status = 0;
        int i = 0;
        int cnt = line.size();
        std::string left, right;
        char op;
        while(i < cnt)
        {
            switch(status)
            {
            case 0:
            {
                if(!isdigit(line[i]))
                {
                    op = line[i];
                    status = 1;
                }
                else left.push_back(line[i++]);
            }
            break;
            case 1:
                i++;
                status = 2;
                break;
            case 2:
                right.push_back(line[i++]);
                break;
            }
        }
        std::cout << std::stoi(left) << " " << std::stoi(right) << " " << op << std::endl;
        return Request(std::stoi(left), std::stoi(right), op);
    }
    ~calclient()
    {}
private:
    uint16_t _serverport;
    std::string _serverip;
    int _sock;
};
